const router = require('koa-router')();
const service = require('../service/serviceService');

router.prefix('/api/wx/service');


/**
 * @api {post} /api/wx/service/getService 获取预约页面的服务列表
 * @apiGroup service
 * @apiName getService
 *
 * @apiSuccess {int} code 返回状态码（1：获取成功，2：没有数据，3：服务器出错）
 * @apiSuccess {String} msg 返回状态码对应的信息
 * @apiSuccess {Object[]} data
 * @apiSuccess {String} data.service_id 服务主键值
 * @apiSuccess {String} data.service_name 服务名
 * @apiSuccess {String} data.service_logo 图标
 */

router.post('/getService', async (ctx) => {
    try {
        let result;
        result = await service.handleGetService();
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});


/**
 * @api {post} /api/wx/service/getServiceById 预约时查看单个服务详细信息
 * @apiGroup service
 * @apiName getServiceById
 *
 * @apiParam {String} service_id 服务主键值.
 *
 * @apiSuccess {int} code 返回状态码（1：获取成功，2：没有数据，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 * @apiSuccess {Object[]} data
 * @apiSuccess {String} data.service_id 服务主键值
 * @apiSuccess {String} data.service_name 服务名
 * @apiSuccess {String} data.service_logo 图标
 * @apiSuccess {String} data.service_introduce 简介
 */

router.post('/getServiceById', async (ctx) => {
    try {
        let data = ctx.request.body;
        let service_id = data.service_id;
        if (!service_id) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
        }
        let result;
        result = await service.handleGetServiceById(data);
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});


/**
 * @api {post} /api/wx/service/addAppointment 预约填写内容
 * @apiGroup service
 * @apiName addAppointment
 *
 * @apiParam {String} openId 小程序openID.
 * @apiParam {String} service_id 服务主键值.
 * @apiParam {String} appointment_people 预约人.
 * @apiParam {String} appointment_phone 联系电话.
 * @apiParam {String} appointment_remark 备注.
 *
 *
 * @apiSuccess {int} code 返回状态码（1：预约成功，2：预约失败，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 */

router.post('/addAppointment', async (ctx) => {
    try {
        let data = ctx.request.body;
        let service_id = data.service_id,
            appointment_people = data.appointment_people,
            appointment_phone = data.appointment_phone,
            appointment_remark = data.appointment_remark,
            openId=data.openId;
        if (!openId||!service_id || !appointment_people || !appointment_phone) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
        }
        let result;
        result = await service.handleAddAppointment(data);
        if (result.code === 1) {
            result.msg = '预约成功！';
        } else if (result.code === 2) {
            result.msg = '预约失败！';
        }
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

/**
 * @api {post} /api/wx/service/getAppointmentByOpenId 订单详情
 * @apiGroup service
 * @apiName getAppointmentByOpenId
 *
 * @apiParam {String} openId 小程序openId.
 *
 *
 * @apiSuccess {int} code 返回状态码（1：获取成功，2：没有数据，3：服务器出错，4：参数不全）
 * @apiSuccess {String} msg 返回状态码对应的信息
 * @apiSuccess {Object[]} data 获取成功时返回的数据
 * @apiSuccess {String} data.service_name 服务名
 * @apiSuccess {String} data.appointment_phone 联系方式
 * @apiSuccess {String} data.appointment_time 预约时间
 * @apiSuccess {String} data.appointment_remark 备注
 * @apiSuccess {String} data.appointment_state 预约状态（-1：取消，0：未处理，1：已处理）
 */

router.post('/getAppointmentByOpenId', async (ctx) => {
    try {
        let data = ctx.request.body;
        let openId = data.openId;
        if (!openId) {
            let data = {code: 4, msg: '参数不全！'};
            ctx.body = data;
        }
        let result;
        result = await service.handleGetAppointmentByOpenId(data);
        ctx.body = result;
    } catch (e) {
        let data = {code: 3, msg: e.message};
        ctx.body = data;
    }
});

module.exports = router;
